home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 June / MACPOWER-1997-06.ISO.7z / MACPOWER-1997-06.ISO / AMUG / PROGRAMMING / BSDSoundLib 1.0.1.sit / BSDSoundLib dist / examples / Looping Sound ƒ / LoopingSound.c < prev    next >
C/C++ Source or Header  |  1997-01-21  |  4KB  |  177 lines

  1. #include <BSDSoundLib.h>
  2. #include <QDOffscreen.h>
  3. #include <Palettes.h>
  4.  
  5. Handle                snd;
  6. StringHandle        fontName;
  7. short                refNum, oldResFile, fontNum;
  8. StandardFileReply    file;
  9. WindowPtr            window, whichWindow;
  10. Boolean                done = false;
  11. EventRecord            event;
  12.  
  13. void main (void);
  14. Boolean OpenSndFile (StandardFileReply *reply, short *refNum);
  15. void SetFontSizeStyle (short fontnum, short fontsize, short fontstyle) ;
  16. void Ellipsize (Str255 s, short width);
  17. void CenterString (Rect r, Str255 s) ;
  18. void EventLoop (void);
  19.  
  20. void Ellipsize (Str255 s, short width) {
  21.     char len;
  22.     short newwidth;
  23.     
  24.     if ((newwidth = StringWidth (s)) <= width)
  25.         return;
  26.     len = s [0]; 
  27.     width -= CharWidth ('ノ');
  28.     do { 
  29.         newwidth -= CharWidth (s [len]);
  30.         --len;
  31.     } while ((newwidth > width) && (len != 0));
  32.     ++len; 
  33.     s [len] = 'ノ'; 
  34.     s [0] = (char) len;
  35. }
  36.  
  37. void CenterString (Rect r, Str255 s) {
  38. GDHandle    device;
  39. RGBColor    whiteRGBColor = {0xFFFF,0xFFFF,0xFFFF}, grayRGBolor = {0x0000, 0x0000, 0x0000};
  40. short rh = r.bottom - r.top;
  41. short rw = r.right - r.left;
  42. short h, v;
  43. FontInfo fi;
  44.     
  45.     GetFontInfo (&fi);
  46.     Ellipsize(s, rw);
  47.     h = r.left + ((rw - StringWidth (s)) / 2);
  48.     v = r.top + ((rh - (fi.ascent + fi.descent)) / 2) + fi.ascent;
  49.     ClipRect (&r);
  50.     
  51.     GetGWorld(nil, &device);
  52.     GetGray(device, &whiteRGBColor, &grayRGBolor);
  53.     
  54.     RGBForeColor(&grayRGBolor);
  55.     MoveTo (h + 3, v + 3);
  56.     DrawString (s);
  57.     
  58.     ForeColor(blackColor);
  59.     MoveTo (h, v);
  60.     DrawString (s);
  61. }
  62.  
  63. static void SetFontSizeStyle (short fontnum, short fontsize, short fontstyle) {
  64.     TextFont (fontnum);
  65.     TextSize (fontsize);
  66.     TextFace (fontstyle);
  67. }
  68.  
  69. Boolean OpenSndFile (StandardFileReply *reply, short *refNum) {
  70. SFTypeList    typeList;
  71.  
  72.     typeList[0] = 'sfil';
  73.     StandardGetFile(nil, 1, typeList, reply);
  74.     if (reply->sfGood) {
  75.         (*refNum) = FSpOpenResFile(&reply->sfFile, fsRdPerm);
  76.         return(true);
  77.     }
  78.     return(false);
  79. }
  80.  
  81. void main (void) {
  82.     MaxApplZone();
  83.     
  84.     InitGraf(&qd.thePort);
  85.     InitFonts();
  86.     InitWindows();
  87.     InitMenus();
  88.     TEInit();
  89.     InitDialogs(nil);
  90.     InitCursor();
  91.     
  92.     FlushEvents(everyEvent, 0);
  93.  
  94.     oldResFile = CurResFile();
  95.     if (!OpenSndFile(&file, &refNum)) ExitToShell();
  96.     
  97.     snd = GetIndResource('snd ', 1);
  98.     HLockHi(snd);
  99.     DetachResource(snd);
  100.     
  101.     CloseResFile(refNum);
  102.     UseResFile(oldResFile);
  103.     
  104.     InitLoopingSounds();
  105.     
  106.     window = GetNewCWindow(128, nil, (WindowPtr)-1L);
  107.     SetPort(window);
  108.     ShowWindow(window);
  109.     
  110.     fontName = GetString(128);
  111.     GetFNum((*fontName), &fontNum);
  112.     DisposeHandle((Handle)fontName);
  113.     
  114.     SetFontSizeStyle(fontNum, 18, normal);
  115.     CenterString(window->portRect, file.sfFile.name);
  116.     
  117.     PlayLoopingSound(snd);
  118.     
  119.     EventLoop();
  120.     
  121.     FlushEvents(everyEvent, 0);
  122.     
  123.     DisposeLoopingSounds();
  124.     
  125.     HUnlock(snd);
  126.     
  127.     DisposeWindow(window);
  128.     
  129.     ExitToShell();
  130. }
  131.  
  132. void EventLoop (void) {
  133.     while (!done) {
  134.         WaitNextEvent(everyEvent, &event, 20, nil);
  135.         switch (event.what) {
  136.             case mouseDown:
  137.                 switch (FindWindow(event.where, &whichWindow)) {
  138.                     case inContent:
  139.                         if (whichWindow != FrontWindow()) SelectWindow(whichWindow);
  140.                     break;
  141.                     case inDrag:
  142.                         DragWindow(whichWindow, event.where, &qd.screenBits.bounds);
  143.                     break;
  144.                     case inSysWindow:
  145.                         SystemClick(&event, whichWindow);
  146.                     break;
  147.                     case inGoAway:
  148.                         if (whichWindow == window) 
  149.                             if (TrackGoAway(whichWindow, event.where)) done = true;
  150.                     break;
  151.                 }
  152.             break;
  153.             case keyDown:
  154.                 if (event.modifiers & cmdKey)
  155.                     switch ((char)(event.message & charCodeMask)) {
  156.                         case 'Q':
  157.                         case 'q':
  158.                         case 'w':
  159.                         case 'W':
  160.                         case '.':
  161.                             done = true;
  162.                         return;
  163.                     }
  164.             break;
  165.             case updateEvt:
  166.                 BeginUpdate((WindowPtr)event.message);
  167.                 if ((WindowPtr)event.message == window) {
  168.                     SetPort(window);
  169.                     CenterString(window->portRect, file.sfFile.name);
  170.                 }
  171.                 EndUpdate((WindowPtr)event.message);
  172.             break;
  173.             default:
  174.             break;
  175.         }
  176.     }
  177. }